package module.building.model
{
	import com.darwin.darwinMVC.core.CmdNameList;
	
	import flash.utils.Dictionary;
	
	import main.AppNet;
	import main.mvc.GModelData;
	
	import module.building.cmds.BuildCommand;
	import module.building.model.dataObj.BuildingObj;
	import module.main.model.dataObj.UserInfoObj;
	import module.scene.cmds.SceneCommand;
	
	public class BuildingModelData extends GModelData
	{
		
		/**
		 * 接收摆放成功后的消息 
		 */		
		private static const GET_BUILD_CONSTRUCTION:String = "building_info";
		
		/**
		 * 发送摆放命令 
		 */		
		private static const SEND_BUILD_CONSTRUCTION:String = "multi_construct";
		
		/**
		 *	发送生产命令 
		 */		
		private static const SEND_PRODUCE_CMD:String = "building_produce";
		
		/**
		 * 发送改变城门状态信息 
		 */		
		private static const SEND_CHANGE_DOOR_STATUS:String = "change_door_status";
		
		/**
		 * 改变建筑信息推送 
		 */		
		private static const GET_CHANGED_BUILDING_INFO :String = "changed_building_info";
		
		/**
		 *	接受生产返回 
		 */	
		private static const GET_PRODUCE_RT:String = "produce_info";
		
		/**
		 *	发送收获命令 
		 */		
		private static const SEND_HARVEST_CMD:String = "building_harvest";
		
		/**
		 *	接受收获返回 
		 */		
		private static const GET_HARVEST_RT:String = "harvest_info";
		
		/**
		 * 接收删除建筑的返回信息命令 
		 */
		private static const GET_REMOVE_CONSTRUCTION:String = "remove_building_info";
		
		/**
		 * 发送删除建筑的命令 
		 */
		private static const SEND_REMOVE_CONSTRUCTION:String = "multi_remove";
		
		/**
		 * 接收移动建筑的返回信息命令 
		 */
		private static const GET_MOVE_CONSTRUCTION:String = "move_building_info";
		
		/**
		 * 发送移动建筑的命令 
		 */
		private static const SEND_MOVE_CONSTRUCTION:String = "building_move";
		
		private static const GET_PRODUCE_LIST_RT:String = "produce_list_info";
		
		private static const GET_UPGRADE_START:String = "upstart";
		
		private static const SEND_UPGRADE_START:String = "upstartinfo";
		
		private static const GET_UPGRADE_END:String = "upcomplete";
		
		private static const SEND_UPGRADE_END:String = "upcompleteinfo";
		
		//===================命令监听列表==========================
		private static const CMDS_BUILD_CONSTRUCTION_NOTICE:CmdNameList  = new CmdNameList(GET_BUILD_CONSTRUCTION);
		private static const CMDS_BUILD_PRODUCTION_RT:CmdNameList = new CmdNameList(GET_PRODUCE_RT);
		private static const CMDS_BUILD_HARVEST_RT:CmdNameList = new CmdNameList(GET_HARVEST_RT);
		private static const CMDS_REMOVE_CONSTRUCTION_NOTICE:CmdNameList = new CmdNameList(GET_REMOVE_CONSTRUCTION);
		private static const CMDS_MOVE_CONSTRUCTION_NOTICE:CmdNameList = new CmdNameList(GET_MOVE_CONSTRUCTION);
		private static const CMDS_PRODUCE_LIST_NOTICE:CmdNameList = new CmdNameList(GET_PRODUCE_LIST_RT);
		private static const CMDS_UPGRADE_START_NOTICE:CmdNameList = new CmdNameList(GET_UPGRADE_START);
		private static const CMDS_UPGRADE_COMPLETE_NOTICE:CmdNameList = new CmdNameList(GET_UPGRADE_END);
		/**
		 * 改变建筑信息 命令列表 
		 */		
		private static var CMDS_CHANGED_BUILDING_INFO:CmdNameList = new CmdNameList(BuildingModelData.GET_CHANGED_BUILDING_INFO);
		
		public function BuildingModelData()
		{
			super();
			init();
		}
		
		private function init():void {
			initRegister();
		}
		
		private function initRegister():void {
			register(CMDS_BUILD_CONSTRUCTION_NOTICE,getBuildConstructionHandler);
			register(CMDS_BUILD_PRODUCTION_RT, getProductionHandler);
			register(CMDS_BUILD_HARVEST_RT, getHarvestHandler);
			register(CMDS_REMOVE_CONSTRUCTION_NOTICE,getRemoveConstructionHandler);
			register(CMDS_MOVE_CONSTRUCTION_NOTICE, getMoveConstructionHandler);
//			register(CMDS_PRODUCE_LIST_NOTICE, getProduceListHandler);
			register(CMDS_UPGRADE_START_NOTICE, getUpgradeStartHandler);
			register(CMDS_UPGRADE_COMPLETE_NOTICE, getUpgradeEndHandler);
			register(CMDS_CHANGED_BUILDING_INFO,getChangeBuildingInfoHandler);
		}
		
		/**
		 * 	处理后后台返回的生产信息列表（登陆时后台自动推送给我）
		 * 	@param data
		 */		
		private function getProduceListHandler(data:Object):void
		{
			trace("登陆后返回生产列表");
			var produceList:Array = data["produce_list_info"];
			var cmdProduceList:BuildCommand = new BuildCommand(BuildCommand.BUILD_PRODUCE);
			cmdProduceList.data = produceList;
			dispatch(cmdProduceList);
		}
		
		/**
		 *	处理后台返回的生产信息(请求生产后返回) 
		 * 	@param data
		 */		
		private function getProductionHandler(data:Object):void
		{
			trace("后台返回，开始生产");
			var cmdData:Object = data["produce_info"];
			var cmd:BuildCommand = new BuildCommand(BuildCommand.BUILD_PRODUCE);
			cmd.data = cmdData;
			dispatch(cmd);
		}
		
		/**
		 *	处理后台返回的收获信息 
		 * 	@param data
		 */		
		private function getHarvestHandler(data:Object):void
		{
			trace("后台返回，开始收获");
			var harvestData:Object = data["harvest_info"];
			var cmdHarvest:BuildCommand = new BuildCommand(BuildCommand.BUILD_HARVEST);
			cmdHarvest.data = harvestData;
			dispatch(cmdHarvest);
		}
		
		private function getBuildConstructionHandler(data:Object):void {
			if(data&&data.building_info){
				var dataAry:Array =[];
				var userInfo:UserInfoObj = appCommonData.userInfo;
				for each(var obj:Object in data.building_info){
					if(obj.uid == userInfo.userId){
						dataAry.push(obj);
					}
				}
				var cmd:BuildCommand = new BuildCommand(BuildCommand.BUILD_CONSTRUCTION_SUCCESS);
				cmd.data = dataAry;
				dispatch(cmd);
			}
		}
		
		/**
		 * 改变城门开关状态命令 
		 * @param eid1 左门的eId
		 * @param eid2 右门的eId
		 * @param openStatus 0为关，1为开
		 * 
		 */		
		public function sendChangeDoorStatus(eid1:String,eid2:String,openStatus:int):void{
			var data:Object = {"eid_list":[eid1,eid2],"open_status":openStatus};
			AppNet.getInstance().send(SEND_CHANGE_DOOR_STATUS,data);
		}
		
		/**
		 * 发送摆放接口 
		 * @param buildList 准备摆放的建筑数组          数据结构  [type:int,x:int,y:int]
		 * 
		 */		
		public function sendBuildConstruction(buildList:Array):void{
			var buildInfo:Array = [];
			var num:int = 0;
			for each(var obj:Object in buildList){
				var info:Object = {};
				info.type = obj.type;
				info.x = obj.x;
				info.y = obj.y;
				buildInfo.push(info);
			}
			var data:Object = {"buiding_list":buildInfo};
			AppNet.getInstance().send(SEND_BUILD_CONSTRUCTION,data);
		}
		
		private function getRemoveConstructionHandler(data:Object):void{
			if(data&&data.remove_building_info){
				var dataAry:Array =[];
				var userInfo:UserInfoObj = appCommonData.userInfo;
				for each(var obj:Object in data.remove_building_info){
					if(obj.uid == userInfo.userId){
						dataAry.push(obj);
					}
				}
				var cmd:BuildCommand = new BuildCommand(BuildCommand.REMOVE_CONSTRUCTION_SUCCESS);
				cmd.data = dataAry;
				dispatch(cmd);
			}
		}
		
		
		private function getChangeBuildingInfoHandler(data:Object):void
		{	
			//TODO 发布iso修改事件
			var $cmd:SceneCommand = new SceneCommand(SceneCommand.SCENE_CMD_MODIFY_ISOOBJECT);
			var $changeList:Array = [];
			for each (var item:Object in data.changed_building_info) 
			{
				var $modifyInfo:Object = {"eId":item.eid,"isoDataObj":{"walkable":item.open_status}};
				$changeList.push($modifyInfo);
			}
			$cmd.data = $changeList;
			dispatch($cmd);
		}
		
		/**
		 * 	向后台发送生产命令
		 * @param value 数据结构[serverId:String, eid:String]
		 */		
		public function sendBuildProduction(value:Object):void
		{
			trace("==========请求生产==========");
			var produceData:Object = value;
			AppNet.getInstance().send(SEND_PRODUCE_CMD, produceData);
		}
		
		/**
		 * 	向后台发送收获命令
		 * @param value 数据结构[eid:String]
		 */		
		public function sendBuildHarvest(value:Object):void
		{
			trace("==========请求收获==========");
			var harvestData:Object = value;
			AppNet.getInstance().send(SEND_HARVEST_CMD, harvestData);
		}
		
		/**
		 * 发送删除接口 
		 * @param buildList
		 * 
		 */
		public function sendRemoveConstruction(buildList:Array):void{
			var buildInfo:Array = [];
			var num:int = 0;
			for each(var obj:Object in buildList){
				var info:Object = {};
				info.eid = obj.eid;
				info.x = obj.x;
				info.y = obj.y;
				buildInfo.push(info)
			}
			var data:Object = {"buiding_list":buildInfo};
			AppNet.getInstance().send(SEND_REMOVE_CONSTRUCTION,data);
		}
		
		/**
		 * 获取建筑移动的数据 
		 * @param data
		 */
		private function getMoveConstructionHandler(data:Object):void{
			if(data&&data.move_building_info){
				var dataAry:Array =[];
				var userInfo:UserInfoObj = appCommonData.userInfo;
				for each(var obj:Object in data.move_building_info){
					if(obj.uid == userInfo.userId){
						dataAry.push(obj);
					}
				}
				var cmd:BuildCommand = new BuildCommand(BuildCommand.MOVE_CONSTRUCTION_SUCCESS);
				cmd.data = dataAry;
				dispatch(cmd);
			}
		}
		
		/**
		 * 向后台发送移动建筑的请求 
		 * @param buildList
		 */
		public function sendMoveConstruction(buildList:Array):void{
			var buildInfo:Array = [];
			var num:int = 0;
			for each(var obj:Object in buildList){
				var info:Object = {};
				info.eid = obj.eid;
				info.from_x = obj.from_x;
				info.from_y = obj.from_y;
				info.to_x = obj.to_x;
				info.to_y = obj.to_y;
				buildInfo.push(info)
//				var data:Object = {"buiding_list":buildInfo};
				AppNet.getInstance().send(SEND_MOVE_CONSTRUCTION,info);
			}
		}
		
		private function getUpgradeStartHandler(data:Object):void{
//			if(data && data.upgrade_building_info){}
			var cmd:BuildCommand = new BuildCommand(BuildCommand.BUILD_UPGRADE_START);
			cmd.data = data;
			dispatch(cmd);
		}
		
		/**
		 * 向后台发送建筑开始升级的请求 
		 * @param data 数据结构[eid:String]
		 * 
		 */
		public function sendUpgradeStart(data:Object):void{
			var upgradeStartData:Object = data;
			getUpgradeStartHandler(data);
		}
		
		private function getUpgradeEndHandler(data:Object):void
		{
			var cmd:BuildCommand = new BuildCommand(BuildCommand.BUILD_UPGRADE_END);
			cmd.data = data;
			dispatch(cmd);
		}
		
		public function sendUpgradeEnd():void{
			
		}
		
		/**
		 * 根据ID获取建筑数据 （已整合造价与等级信息）
		 * @param id 建筑的serverId
		 * @return 建筑数据强类型
		 */
		public function getBuildingObjById(id:int):BuildingObj
		{
			return buildingConfigDic[id] as BuildingObj;
		}
		
		/**
		 * 根据ID和等级获取建筑等级信息 （建筑数据已整合，不必刻意调用）
		 */
		public function getBuildingLevelInfoById(id:int,level:int):Object
		{
			return buildingLevelDic[id][level-1] as Object;
		}
		
		/**
		 * 根据ID获取建筑造价信息（建筑数据已整合，不必刻意调用） 
		 */
		public function getBuildingPriceById(id:int):Object
		{
			return buildingPriceDic[id] as Object;
		}
		
		/**
		 * 获取全部建筑信息 
		 */
		public function get buildingConfigArr():Array
		{
			return getDataBySource("building") as Array;
		}
		
		/**
		 * 获取全部等级信息 
		 */
		private function get buildingLevelArr():Array
		{
			return getDataBySource("buildingLevel") as Array;
		}		
		
		/**
		 * 获取全部造价信息（其实不仅仅是建筑）
		 */
		private function get buildingPriceArr():Array
		{
			return getDataBySource("commodity") as Array;
		}
		
		/**
		 * 不用这个方法，获取建筑信息请用buildingConfigArr()
		 */
		public function get buildingInfo():Array
		{
			var returnArray:Array = new Array();
			for each(var o:Object in buildingConfigDic)
			{
				var build:Object = getBuildingObjById(o.serverId);
				var level:Object = getBuildingLevelInfoById(o.serverId,1);
				var price:Object = getBuildingPriceById(o.serverId);
				returnArray.push({"build":build,"price":price,"level":level});
			}
			return returnArray;
		}
		
		private var _buildingConfigDic:Dictionary;
		private function get buildingConfigDic():Dictionary
		{
			if(!_buildingConfigDic)
			{
				_buildingConfigDic = new Dictionary();
				var len:int = buildingConfigArr.length;				
				for(var i:int=0;i<len;i++)
				{
					_buildingConfigDic[buildingConfigArr[i].serverId] = BuildingObj.parseData(buildingConfigArr[i]);
				}
			}
			return _buildingConfigDic;
		}
		
		private var _buildingLevelDic:Dictionary;
		private function get buildingLevelDic():Dictionary
		{
			if(!_buildingLevelDic)
			{
				_buildingLevelDic = new Dictionary();
				var len:int = buildingLevelArr.length;
				for(var i:int=0;i<len;i++)
				{
					if(!_buildingLevelDic[buildingLevelArr[i].serverId])
					{
						_buildingLevelDic[buildingLevelArr[i].serverId] = new Dictionary()
					}
					_buildingLevelDic[buildingLevelArr[i].serverId][buildingLevelArr[i].level] = buildingLevelArr[i];
				}
			}
			return _buildingLevelDic;			
		}
		
		private var _buildingPriceDic:Dictionary;
		private function get buildingPriceDic():Dictionary
		{
			if(!_buildingPriceDic)
			{
				_buildingPriceDic = new Dictionary();
				var len:int = buildingPriceArr.length;
				for(var i:int=0;i<len;i++)
				{
					if(!_buildingPriceDic[buildingPriceArr[i].serverId])
					{
						_buildingPriceDic[buildingPriceArr[i].serverId] = new Dictionary()
					}
					_buildingPriceDic[buildingPriceArr[i].serverId][buildingPriceArr[i].level] = buildingPriceArr[i];
				}
			}
			return _buildingPriceDic;			
		}
		/**
		 *得到士兵，装备配置 
		 * @return 
		 * 
		 */		
		public function get armsConfigArr():Array
		{
			return this.getDataBySource("arms") as Array;
		}
		/**
		 *得到技能配置 
		 * @return 
		 * 
		 */		
		public function get skillConfigArr():Array
		{
			return this.getDataBySource("skill") as Array;
		}
		public function get orderConfigArr():Array
		{
			return this.getDataBySource("order") as Array;
		}
	}
}